from PyQt4 import QtCore, QtGui
from ui_metadataeditorwidget import Ui_MetaDataEditorWidget
import sys
import batch_tile
import amara
import contactdialog
import pickle
import string
from amara import binderytools

class Dialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)

        self.ui = Ui_MetaDataEditorWidget()
        self.ui.setupUi(self)
        
        self.contact_dialog = contactdialog.ContactDialog()
        
        self.connect(self.contact_dialog, QtCore.SIGNAL("changed()"),
                     self.load_contacts)

        self.connect(self.ui.editMetadataContactPushButton, QtCore.SIGNAL("clicked()"),
                     self.contact_dialog.show)
        self.connect(self.ui.editDistributionContactPushButton, QtCore.SIGNAL("clicked()"),
                     self.contact_dialog.show)

        self.connect(self.ui.linkageToolButton, QtCore.SIGNAL("clicked()"),
                     self.set_linkage)

        self.connect(self.ui.distributionContactComboBox, QtCore.SIGNAL("currentIndexChanged(int)"),
                     self.change_distribution_contact)
        self.connect(self.ui.metadataContactComboBox, QtCore.SIGNAL("currentIndexChanged(int)"),
                     self.change_metadata_contact)

        self.ui.tabWidget.setCurrentIndex(0)

        self.load_contacts()

    def change_distribution_contact(self, index):
        self.ui.distributionContactTextBrowser.setText(
            "Contact Organization: %s\n" % self.contacts[index]['organization'] +
            "Contact Person: %s\n" % self.contacts[index]['person'] +
            "Address Type: %s\n" % self.contacts[index]['addresstype'] +
            "Address: %s\n" % self.contacts[index]['address'] +
            "City: %s\n" % self.contacts[index]['city'] +
            "State or Province: %s\n" % self.contacts[index]['state'] +
            "Postal Code: %s\n" % self.contacts[index]['postalcode'] +
            "Country: %s\n" % self.contacts[index]['country'] +
            "Voice: %s\n" % self.contacts[index]['voice'] +
            "Fax: %s\n" % self.contacts[index]['fax'] +
            "Email: %s" % self.contacts[index]['email'])

    def change_metadata_contact(self, index):
        self.ui.metadataContactTextBrowser.setText(
            "Contact Organization: %s\n" % self.contacts[index]['organization'] +
            "Contact Person: %s\n" % self.contacts[index]['person'] +
            "Address Type: %s\n" % self.contacts[index]['addresstype'] +
            "Address: %s\n" % self.contacts[index]['address'] +
            "City: %s\n" % self.contacts[index]['city'] +
            "State or Province: %s\n" % self.contacts[index]['state'] +
            "Postal Code: %s\n" % self.contacts[index]['postalcode'] +
            "Country: %s\n" % self.contacts[index]['country'] +
            "Voice: %s\n" % self.contacts[index]['voice'] +
            "Fax: %s\n" % self.contacts[index]['fax'] +
            "Email: %s" % self.contacts[index]['email'])

    def load_contacts(self):
        try:
            file = open('contacts.dat')
            self.contacts = pickle.load(file)
            self.ui.distributionContactComboBox.clear()
            self.ui.metadataContactComboBox.clear()

            index = 0
            for contact in self.contacts:
                self.ui.distributionContactComboBox.addItem('%d - %s - %s' % (index, 
                                                                              contact['person'],
                                                                              contact['organization']))
                self.ui.metadataContactComboBox.addItem('%d - %s - %s' % (index, 
                                                                              contact['person'],
                                                                              contact['organization']))

                index = index + 1
            file.close()

        except IOError:
            print 'Database file not found.'

    def sizeHint(self):
        return QtCore.QSize(665, 461)

    def set_linkage(self):
        filename = QtGui.QFileDialog.getOpenFileName(None, "Open File", ".", "MrSID files (*.sid);;All Files (*.*)")
        if filename != '':
            self.ui.onlinkLineEdit.setText(filename)

    def load_file(self, filename):
        rules = [binderytools.simple_string_element_rule(u'abstract')]
        doc = amara.parse(open(filename, 'r'), rules=rules)
        self.ui.originLineEdit.setText(unicode(doc.metadata.idinfo.citation.citeinfo.origin))
#        self.ui.pubdateDateEdit.setDate(QtCore.QDate.fromString(str(doc.metadata.idinfo.citation.citeinfo.pubdate),
#                                                                'yyyyMMdd'))
        self.ui.pubdateLineEdit.setText(str(doc.metadata.idinfo.citation.citeinfo.pubdate))
        self.ui.titleLineEdit.setText(unicode(doc.metadata.idinfo.citation.citeinfo.title))

        if doc.metadata.idinfo.citation.citeinfo.xml_child_elements.has_key('edition'):
            self.ui.editionLineEdit.setText(str(doc.metadata.idinfo.citation.citeinfo.edition))
        
        if doc.metadata.xml_child_elements.has_key('dataqual'):
            self.ui.srcscaleLineEdit.setText(str(doc.metadata.dataqual.lineage.srcinfo.srcscale))

        if doc.metadata.idinfo.citation.citeinfo.xml_child_elements.has_key('geoform'):
            index = self.ui.geoformComboBox.findText(str(doc.metadata.idinfo.citation.citeinfo.geoform))
            self.ui.geoformComboBox.setCurrentIndex(index)

        if doc.metadata.idinfo.citation.citeinfo.xml_child_elements.has_key('pubinfo'):
            self.ui.pubplaceLineEdit.setText(unicode(doc.metadata.idinfo.citation.citeinfo.pubinfo.pubplace))
            self.ui.publishLineEdit.setText(unicode(doc.metadata.idinfo.citation.citeinfo.pubinfo.publish))

        if doc.metadata.idinfo.citation.citeinfo.xml_child_elements.has_key('onlink'):
            self.ui.onlinkLineEdit.setText(str(doc.metadata.idinfo.citation.citeinfo.onlink))

        self.ui.abstractTextEdit.setText(doc.metadata.idinfo.descript.abstract)
        self.ui.purposeTextEdit.setText(str(doc.metadata.idinfo.descript.purpose))

        if doc.metadata.idinfo.descript.xml_child_elements.has_key('supplinf'):
            self.ui.supplinfTextEdit.setText(str(doc.metadata.idinfo.descript.supplinf))

        #self.ui.begdateDateEdit.setDate(QtCore.QDate.fromString(str(doc.metadata.idinfo.timeperd.timeinfo.rngdates.begdate),
        #                                                        'yyyyMMdd'))
        #self.ui.enddateDateEdit.setDate(QtCore.QDate.fromString(str(doc.metadata.idinfo.timeperd.timeinfo.rngdates.enddate),
        #                                                        'yyyyMMdd'))
        self.ui.begdateLineEdit.setText(str(doc.metadata.idinfo.timeperd.timeinfo.rngdates.begdate))
        self.ui.enddateLineEdit.setText(str(doc.metadata.idinfo.timeperd.timeinfo.rngdates.enddate))
        
        index = self.ui.currentComboBox.findText(str(doc.metadata.idinfo.timeperd.current))
        self.ui.currentComboBox.setCurrentIndex(index)

        index = self.ui.progressComboBox.findText(str(doc.metadata.idinfo.status.progress))
        self.ui.progressComboBox.setCurrentIndex(index)

        index = self.ui.updateComboBox.findText(str(doc.metadata.idinfo.status.update))
        self.ui.updateComboBox.setCurrentIndex(index)

        self.ui.westbcLineEdit.setText(str(doc.metadata.idinfo.spdom.bounding.westbc))
        self.ui.eastbcLineEdit.setText(str(doc.metadata.idinfo.spdom.bounding.eastbc))
        self.ui.northbcLineEdit.setText(str(doc.metadata.idinfo.spdom.bounding.northbc))
        self.ui.southbcLineEdit.setText(str(doc.metadata.idinfo.spdom.bounding.southbc))

        if doc.metadata.xml_child_elements.has_key('spdoinfo'):
            if doc.metadata.spdoinfo.xml_child_elements.has_key('rastinfo'):
                self.ui.colcountSpinBox.setValue(int(str(doc.metadata.spdoinfo.rastinfo.colcount)))
                self.ui.rowcountSpinBox.setValue(int(str(doc.metadata.spdoinfo.rastinfo.rowcount)))

#        if doc.metadata.xml_child_elements.has_key('spref'):
#            if doc.metadata.spref.horizsys.planar.xml_child_elements.has_key('mapproj'):
#                index = self.ui.mapprojnComboBox.findText(str(doc.metadata.spref.horizsys.
#                                                              planar.mapproj.mapprojn))
#                self.ui.mapprojnComboBox.setCurrentIndex(index)

        self.ui.themektLineEdit.setText(str(doc.metadata.idinfo.keywords.theme.themekt))
        if doc.metadata.idinfo.keywords.xml_child_elements.has_key('theme'):
            themekeys = string.join(map(str, doc.metadata.idinfo.keywords.theme.themekey), '; ')
            self.ui.themekeyLineEdit.setText(themekeys)

        self.ui.placektLineEdit.setText(str(doc.metadata.idinfo.keywords.place.placekt))
        if doc.metadata.idinfo.keywords.xml_child_elements.has_key('place'):
            placekeys = string.join(map(str, doc.metadata.idinfo.keywords.place.placekey), '; ')
            self.ui.placekeyLineEdit.setText(placekeys)

#        if doc.metadata.idinfo.keywords.xml_child_elements.has_key('temporal'):
#            self.ui.temporalktLineEdit.setText(str(doc.metadata.idinfo.keywords.temporal.temporalkt))
#            self.ui.temporalkeyLineEdit.setText(str(doc.metadata.idinfo.keywords.temporal.temporalkey))

        cntper = str(doc.metadata.distinfo.distrib.cntinfo.cntperp.cntper)
        for c in self.contacts:
            if cntper == c['person']:
                index = self.contacts.index(c)
                self.ui.distributionContactComboBox.setCurrentIndex(index)

        if doc.metadata.distinfo.xml_child_elements.has_key('resdesc'):
            self.ui.resdescLineEdit.setText(str(doc.metadata.distinfo.resdesc))

        self.ui.distliabTextEdit.setText(str(doc.metadata.distinfo.distliab))

        self.ui.accconstTextEdit.setText(str(doc.metadata.idinfo.accconst))
        self.ui.useconstTextEdit.setText(str(doc.metadata.idinfo.useconst))

#        if doc.metadata.idinfo.xml_child_elements.has_key('browse'):
#            self.ui.browsenLineEdit.setText(str(doc.metadata.idinfo.browse.browsen))
#            self.ui.browsedLineEdit.setText(str(doc.metadata.idinfo.browse.browsed))
#            index = self.ui.browsetComboBox.findText(str(doc.metadata.idinfo.browse.browset))
#            self.ui.browsetComboBox.setCurrentIndex(index)

        self.ui.metdDateEdit.setDate(QtCore.QDate.fromString(str(doc.metadata.metainfo.metd),
                                                             'yyyyMMdd'))

        cntper = str(doc.metadata.metainfo.metc.cntinfo.cntperp.cntper)
        for c in self.contacts:
            if cntper == c['person']:
                index = self.contacts.index(c)
                self.ui.metadataContactComboBox.setCurrentIndex(index)

        self.setWindowTitle(filename)

    def write_file(self, filename):
        doc = amara.create_document(u'metadata')

        idinfo = doc.xml_create_element(u'idinfo')
        citation = doc.xml_create_element(u'citation')
        citeinfo = doc.xml_create_element(u'citeinfo')

        origin = doc.xml_create_element(u'origin')
        origin.xml_append(unicode(self.ui.originLineEdit.text()))
        citeinfo.xml_append(origin)

        pubdate = doc.xml_create_element(u'pubdate')
#        pubdate.xml_append(unicode(self.ui.pubdateDateEdit.date().toString('yyyyMMdd')))
        pubdate.xml_append(unicode(self.ui.pubdateLineEdit.text()))
        citeinfo.xml_append(pubdate)

        title = doc.xml_create_element(u'title')
        title.xml_append(unicode(self.ui.titleLineEdit.text()))
        citeinfo.xml_append(title)

        geoform = doc.xml_create_element(u'geoform')
        geoform.xml_append(unicode(self.ui.geoformComboBox.currentText()))
        citeinfo.xml_append(geoform)

        pubinfo = doc.xml_create_element(u'pubinfo')
        pubplace = doc.xml_create_element(u'pubplace')
        pubplace.xml_append(unicode(self.ui.pubplaceLineEdit.text()))
        publish = doc.xml_create_element(u'publish')
        publish.xml_append(unicode(self.ui.publishLineEdit.text()))
        pubinfo.xml_append(pubplace)
        pubinfo.xml_append(publish)
        citeinfo.xml_append(pubinfo)        

        onlink = doc.xml_create_element(u'onlink')
        onlink.xml_append(unicode(self.ui.onlinkLineEdit.text()))
        edition = doc.xml_create_element(u'edition')
        edition.xml_append(unicode(self.ui.editionLineEdit.text()))
        citeinfo.xml_append(edition)
        citeinfo.xml_append(onlink)

        citation.xml_append(citeinfo)
        idinfo.xml_append(citation)

        descript = doc.xml_create_element(u'descript')
        abstract = doc.xml_create_element(u'abstract')
        abstract.xml_append(unicode(self.ui.abstractTextEdit.toPlainText()))
        descript.xml_append(abstract)
        purpose = doc.xml_create_element(u'purpose')
        purpose.xml_append(unicode(self.ui.purposeTextEdit.toPlainText()))
        descript.xml_append(purpose)
        supplinf = doc.xml_create_element(u'supplinf')
        supplinf.xml_append(unicode(self.ui.supplinfTextEdit.toPlainText()))
        descript.xml_append(supplinf)

        idinfo.xml_append(descript)

        timeperd = doc.xml_create_element(u'timeperd')
        timeinfo = doc.xml_create_element(u'timeinfo')
        rngdates = doc.xml_create_element(u'rngdates')
        begdate = doc.xml_create_element(u'begdate')
#        begdate.xml_append(unicode(self.ui.begdateDateEdit.date().toString('yyyyMMdd')))
        begdate.xml_append(unicode(self.ui.begdateLineEdit.text()))
        rngdates.xml_append(begdate)
        enddate = doc.xml_create_element(u'enddate')
#        enddate.xml_append(unicode(self.ui.enddateDateEdit.date().toString('yyyyMMdd')))
        enddate.xml_append(unicode(self.ui.enddateLineEdit.text()))
        rngdates.xml_append(enddate)
        timeinfo.xml_append(rngdates)
        timeperd.xml_append(timeinfo)

        current = doc.xml_create_element(u'current')
        current.xml_append(unicode(self.ui.currentComboBox.currentText()))
        timeperd.xml_append(current)
        idinfo.xml_append(timeperd)

        status = doc.xml_create_element(u'status')
        progress = doc.xml_create_element(u'progress')
        progress.xml_append(unicode(self.ui.progressComboBox.currentText()))
        status.xml_append(progress)
        update = doc.xml_create_element(u'update')
        update.xml_append(unicode(self.ui.updateComboBox.currentText()))
        status.xml_append(update)
        idinfo.xml_append(status)

        spdom = doc.xml_create_element(u'spdom')
        bounding = doc.xml_create_element(u'bounding')
        westbc = doc.xml_create_element(u'westbc')
        westbc.xml_append(unicode(self.ui.westbcLineEdit.text()))
        bounding.xml_append(westbc)
        eastbc = doc.xml_create_element(u'eastbc')
        eastbc.xml_append(unicode(self.ui.eastbcLineEdit.text()))
        bounding.xml_append(eastbc)
        northbc = doc.xml_create_element(u'northbc')
        northbc.xml_append(unicode(self.ui.northbcLineEdit.text()))
        bounding.xml_append(northbc)
        southbc = doc.xml_create_element(u'southbc')
        southbc.xml_append(unicode(self.ui.southbcLineEdit.text()))
        bounding.xml_append(southbc)
        spdom.xml_append(bounding)
        idinfo.xml_append(spdom)

        keywords = doc.xml_create_element(u'keywords')
        theme = doc.xml_create_element(u'theme')
        themekt = doc.xml_create_element(u'themekt')
        themekt.xml_append(unicode(self.ui.themektLineEdit.text()))
        theme.xml_append(themekt)

        themekeys = self.ui.themekeyLineEdit.text().split(';')
        for key in themekeys:
            themekey = doc.xml_create_element(u'themekey')
            themekey.xml_append(unicode(key).strip())
            theme.xml_append(themekey)

        keywords.xml_append(theme)
        place = doc.xml_create_element(u'place')
        placekt = doc.xml_create_element(u'placekt')
        placekt.xml_append(unicode(self.ui.placektLineEdit.text()))
        place.xml_append(placekt)

        placekeys = self.ui.placekeyLineEdit.text().split(';')
        for key in placekeys:
            placekey = doc.xml_create_element(u'placekey')
            placekey.xml_append(unicode(key).strip())
            place.xml_append(placekey)

        keywords.xml_append(place)
#        temporal = doc.xml_create_element(u'temporal')
#        temporalkt = doc.xml_create_element(u'temporalkt')
#        temporalkt.xml_append(unicode(self.ui.temporalktLineEdit.text()))
#        temporal.xml_append(temporalkt)
#        temporalkey = doc.xml_create_element(u'temporalkey')
#        temporalkey.xml_append(unicode(self.ui.temporalkeyLineEdit.text()))
#        temporal.xml_append(temporalkey)
#        keywords.xml_append(temporal)
        idinfo.xml_append(keywords)

        accconst = doc.xml_create_element(u'accconst')
        accconst.xml_append(unicode(self.ui.accconstTextEdit.toPlainText()))
        idinfo.xml_append(accconst)

        useconst = doc.xml_create_element(u'useconst')
        useconst.xml_append(unicode(self.ui.useconstTextEdit.toPlainText()))
        idinfo.xml_append(useconst)

#        browse = doc.xml_create_element(u'browse')
#        browsen = doc.xml_create_element(u'browsen')
#        browsen.xml_append(unicode(self.ui.browsenLineEdit.text()))
#        browse.xml_append(browsen)
#        browsed = doc.xml_create_element(u'browsed')
#        browsed.xml_append(unicode(self.ui.browsedLineEdit.text()))
#        browse.xml_append(browsed)
#        browset = doc.xml_create_element(u'browset')
#        browset.xml_append(unicode(self.ui.browsetComboBox.currentText()))
#        browse.xml_append(browset)
#        idinfo.xml_append(browse)

        doc.metadata.xml_append(idinfo)

        dataqual = doc.xml_create_element(u'dataqual')
        logic = doc.xml_create_element(u'logic')
        dataqual.xml_append(logic)
        complete = doc.xml_create_element(u'complete')
        dataqual.xml_append(complete)
        
        lineage = doc.xml_create_element(u'lineage')
        srcinfo = doc.xml_create_element(u'srcinfo')
        srcscale = doc.xml_create_element(u'srcscale')
        srcscale.xml_append(unicode(self.ui.srcscaleLineEdit.text()))
        srcinfo.xml_append(srcscale)
        typesrc = doc.xml_create_element(u'typesrc')
        srcinfo.xml_append(typesrc)
        srctime = doc.xml_create_element(u'srctime')

        srccurr = doc.xml_create_element(u'srccurr')
        srctime.xml_append(srccurr)

        srctime.xml_append(timeinfo)

        srcinfo.xml_append(srctime)
        srccite = doc.xml_create_element(u'srccite')
        srccite.xml_append(citeinfo)

        srcinfo.xml_append(srccite)
        srccitea = doc.xml_create_element(u'srccitea')
        srcinfo.xml_append(srccitea)
        srccontr = doc.xml_create_element(u'srccontr')
        srcinfo.xml_append(srccontr)
        
        lineage.xml_append(srcinfo)

        procstep = doc.xml_create_element(u'procstep')
        procdesc = doc.xml_create_element(u'procdesc')
        procdate = doc.xml_create_element(u'procdate')
        procstep.xml_append(procdesc)
        procstep.xml_append(procdate)
        lineage.xml_append(procstep)

        dataqual.xml_append(lineage)
        doc.metadata.xml_append(dataqual)

        spdoinfo = doc.xml_create_element(u'spdoinfo')
        direct = doc.xml_create_element(u'direct')
        direct.xml_append(unicode('Raster'))
        spdoinfo.xml_append(direct)
        rastinfo = doc.xml_create_element(u'rastinfo')
        rowcount = doc.xml_create_element(u'rowcount')
        rowcount.xml_append(unicode(str(self.ui.rowcountSpinBox.value())))
        rastinfo.xml_append(rowcount)
        colcount = doc.xml_create_element(u'colcount')
        colcount.xml_append(unicode(str(self.ui.colcountSpinBox.value())))
        rastinfo.xml_append(colcount)
        rasttype = doc.xml_create_element(u'rasttype')
        rasttype.xml_append(unicode('Pixel'))
        rastinfo.xml_append(rasttype)
        spdoinfo.xml_append(rastinfo)
        
        doc.metadata.xml_append(spdoinfo)

#        spref = doc.xml_create_element(u'spref')
#        horizsys = doc.xml_create_element(u'horizsys')
#        planar = doc.xml_create_element(u'planar')
#        mapproj = doc.xml_create_element(u'mapproj')
#        mapprojn = doc.xml_create_element(u'mapprojn')
#        mapprojn.xml_append(unicode(self.ui.mapprojnComboBox.currentText()))
#        mapproj.xml_append(mapprojn)
#        planar.xml_append(mapproj)
#        horizsys.xml_append(planar)
#        spref.xml_append(horizsys)
#        doc.metadata.xml_append(spref)

        distinfo = doc.xml_create_element(u'distinfo')
        distrib = doc.xml_create_element(u'distrib')
        cntinfo = doc.xml_create_element(u'cntinfo')
        cntperp = doc.xml_create_element(u'cntperp')
        contact = self.contacts[self.ui.distributionContactComboBox.currentIndex()]
        cntper = doc.xml_create_element(u'cntper')
        cntper.xml_append(unicode(contact['person']))
        cntperp.xml_append(cntper)
        cntorg = doc.xml_create_element(u'cntorg')
        cntorg.xml_append(unicode(contact['organization']))

        cntaddr = doc.xml_create_element(u'cntaddr')
        addrtype = doc.xml_create_element(u'addrtype')
        addrtype.xml_append(unicode(contact['addresstype']))
        cntaddr.xml_append(addrtype)
        address = doc.xml_create_element(u'address')
        address.xml_append(unicode(contact['address']))
        cntaddr.xml_append(address)
        city = doc.xml_create_element(u'city')
        city.xml_append(unicode(contact['city']))
        cntaddr.xml_append(city)
        state = doc.xml_create_element(u'state')
        state.xml_append(unicode(contact['state']))
        cntaddr.xml_append(state)
        postal = doc.xml_create_element(u'postal')
        postal.xml_append(unicode(contact['postalcode']))
        cntaddr.xml_append(postal)
        country = doc.xml_create_element(u'country')
        country.xml_append(unicode(contact['country']))
        cntaddr.xml_append(country)
        
        cntinfo.xml_append(cntperp)
        cntinfo.xml_append(cntaddr)

        cntvoice = doc.xml_create_element(u'cntvoice')
        cntvoice.xml_append(unicode(contact['voice']))
        cntinfo.xml_append(cntvoice)
        cntfax = doc.xml_create_element(u'cntfax')
        cntfax.xml_append(unicode(contact['fax']))
        cntinfo.xml_append(cntfax)
        cntemail = doc.xml_create_element(u'cntemail')
        cntemail.xml_append(unicode(contact['email']))
        cntinfo.xml_append(cntemail)

        distrib.xml_append(cntinfo)

        distinfo.xml_append(distrib)

        resdesc = doc.xml_create_element(u'resdesc')
        resdesc.xml_append(unicode(str(self.ui.resdescLineEdit.text())))
        distinfo.xml_append(resdesc)

        distliab = doc.xml_create_element(u'distliab')
        distliab.xml_append(unicode(str(self.ui.distliabTextEdit.toPlainText())))
        distinfo.xml_append(distliab)

        doc.metadata.xml_append(distinfo)

        metainfo = doc.xml_create_element(u'metainfo')
        metd = doc.xml_create_element(u'metd')
        metd.xml_append(unicode(self.ui.metdDateEdit.date().toString('yyyyMMdd')))        
        metainfo.xml_append(metd)

        metc = doc.xml_create_element(u'metc')
        cntinfo = doc.xml_create_element(u'cntinfo')
        cntperp = doc.xml_create_element(u'cntperp')
        contact = self.contacts[self.ui.metadataContactComboBox.currentIndex()]
        cntper = doc.xml_create_element(u'cntper')
        cntper.xml_append(unicode(contact['person']))
        cntperp.xml_append(cntper)
        cntorg = doc.xml_create_element(u'cntorg')
        cntorg.xml_append(unicode(contact['organization']))

        cntaddr = doc.xml_create_element(u'cntaddr')
        addrtype = doc.xml_create_element(u'addrtype')
        addrtype.xml_append(unicode(contact['addresstype']))
        cntaddr.xml_append(addrtype)
        address = doc.xml_create_element(u'address')
        address.xml_append(unicode(contact['address']))
        cntaddr.xml_append(address)
        city = doc.xml_create_element(u'city')
        city.xml_append(unicode(contact['city']))
        cntaddr.xml_append(city)
        state = doc.xml_create_element(u'state')
        state.xml_append(unicode(contact['state']))
        cntaddr.xml_append(state)
        postal = doc.xml_create_element(u'postal')
        postal.xml_append(unicode(contact['postalcode']))
        cntaddr.xml_append(postal)
        country = doc.xml_create_element(u'country')
        country.xml_append(unicode(contact['country']))
        cntaddr.xml_append(country)
        
        cntinfo.xml_append(cntperp)
        cntinfo.xml_append(cntaddr)

        cntvoice = doc.xml_create_element(u'cntvoice')
        cntvoice.xml_append(unicode(contact['voice']))
        cntinfo.xml_append(cntvoice)
        cntfax = doc.xml_create_element(u'cntfax')
        cntfax.xml_append(unicode(contact['fax']))
        cntinfo.xml_append(cntfax)
        cntemail = doc.xml_create_element(u'cntemail')
        cntemail.xml_append(unicode(contact['email']))
        cntinfo.xml_append(cntemail)

        metc.xml_append(cntinfo)

        metstdn = doc.xml_create_element(u'metstdn')
        metstdn.xml_append(u'FGDC Content Standard for Digital Geospatial Metadata')
        metainfo.xml_append(metstdn)
        
        metstdv = doc.xml_create_element(u'metstdv')
        metstdv.xml_append(u'FGDC-STD-001-1998')
        metainfo.xml_append(metstdv)

        metainfo.xml_append(metc)
        
        doc.metadata.xml_append(metainfo)

        file = open(filename, 'w')
        file.write(doc.xml(indent=u'yes'))
        
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Dialog()
    window.show()
    window.load_file('testout.xml')
#    window.write_file('testout.xml')
    sys.exit(app.exec_())
    
